#include <stdlib.h>  
#include <stdio.h>  

// This demonstrates the observer design pattern
// Here we decouple the code by using seperate event and eventhandler code
// We register one or more observers and they watch a subject for a predefined change
// When a change occures a event function is called and a event is fired
// The event is then handled by one or more objects and this means or more objects call 
// a eventhandler function

// Eventhandler definition
typedef struct{
	// Variable to keep track of the number of observers
	int NumberOfObservers;

	// Array of pointer functions (return type void, parameter integer)
	void(*ListOfObservers[10])(int);
}EventHandler;

// Function to initialize a handler 
void InitEventHandler(EventHandler *Handler){
	Handler->NumberOfObservers = 0;
}

// Function to register a event handler function
void RegisterObserver(EventHandler *Handler, void(*HandlerFunction)(int)){

	// Add the given handler function to the Eventhandeler
	Handler->ListOfObservers[Handler->NumberOfObservers++] = HandlerFunction;
}

// Update function, this function should be called when the observed object (the event handler) 
// changes status. With this function call to fire a event to notify the observer objects that
// handle the actual events
void UpdateHandler(EventHandler *Handler, int Value){

	// The status has changed, so notify all the registred observers
	for (int Index = 0; Index < Handler->NumberOfObservers; Index++){
		Handler->ListOfObservers[Index](Value);
	}
}

// This function should be part of a seperate software component
// A example could be a printer component, a function is called 
// for example when there is a paper jam event
void HandlerFunction(int Value){
	printf("%s%d\n", "Handler function: ", Value);
}